/*
 * Created by SharpDevelop.
 * User: Rogerio
 * Date: 18/11/2008
 * Time: 10:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Data;
using csharp.common;
using System.Collections.Generic;

namespace csharp.rad.finan
{
	/// <summary>
	/// Description of Group.
	/// </summary>
	public class SubClass : DefaultClass, IClass
	{
		//private String   _id;
		private String   _code;
		private String   _name;
		private DateTime _register_date;
		private bool     _active;
		private String   _class_id;
		
		public SubClass()
		{
			gsql.table= "finan.subclass";
			_sequence = "finan.subclass_subclass_id_seq";
		}
		
		public String code
		{
			set { _code = value; }
			get { return  _code; }
		}
		
		public String name
		{
			set { _name = value; }
			get { return  _name; }
		}
		
		public DateTime registerDate
		{
			set { _register_date = value; }
			get { return  _register_date; }
		}
		
		public bool active
		{
			set { _active = value; }
			get { return  _active; }
		}
		
		public String classId
		{
			set { _class_id = value; }
			get { return  _class_id; }
		}
		
		/// <summary>
		/// Clear the object atributes.
		/// </summary>	
		public void clear()
		{
			_id			   = String.Empty;
			_code          = String.Empty;
			_name          = String.Empty;
			_register_date = DateTime.Now;
			_active        = true;
			_class_id      = String.Empty;
		}
	
	
		/// <summary>
		/// Fill the subclass with data of DataRow
		/// </summary>
		/// <param name="dr">DataRow with the data to fill the subclass.</param>				
		public override void get(DataRow dr)
		{
			_id       		= dr["subclass_id"].ToString();
			_code     		= dr["subclass_code"].ToString();
			_name     		= dr["subclass_name"].ToString();
			_register_date = Convert.ToDateTime(dr["subclass_register_date"]);
			_active   		= Convert.ToBoolean(dr["subclass_active"]);  
			_class_id      = dr["subclass_class_id"].ToString();
		}
		
		
		public override void setGsql()
		{
			gsql.setValue(_id, "subclass_id");
			gsql.setValue(_code, "subclass_code");
			gsql.setValue(_name, "subclass_name");
			//gsql.setValue(_register_date.ToString("dd/MM/yyyy"), "group_register_date");
			gsql.setValue(_active.ToString(), "subclass_active");
			gsql.setValue(_class_id, "subclass_class_id");
		}
	}


	//List
	public class SubClasss : DefaultList, IList
	{		
		public SubClasss()
		{
			gsql.table= "finan.v_subclass";
		}
		
		public SubClass get(Int32 index)
		{
			return (SubClass)items[index];
		}
		
		public SubClass get()
		{
			return (SubClass)items[index];
		}
		
		public bool fillList()
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.additionalWhere = "ORDER BY subclass_code";
			return mountList();
		}
		
		public bool fillList(String classId)
		{
			gsql.prepare();
			gsql.setValue("","*");
			gsql.conditionalWhere = "subclass_class_id = "+classId;
			gsql.additionalWhere = "ORDER BY subclass_code";
			
			return mountList();
		}		
		
		private bool mountList()
		{
			Int32 tam;
			
			if (!gsql.execute())
				return false;
			
			tam= gsql.getRowCount();
			clear();
			for (int i=0; i < tam; i++)
			{
				SubClass g = new SubClass();
				g.get(gsql.getRow(i));
				items.Add(g);
			}
			return true;
		}
		
		public void filterByClass(string classId)
		{
			int tam = items.Count;
			int i;
			
			filtered.Clear();
			for(i=0; i < tam; i++)
			{
				if (((SubClass)items[i]).classId.Equals(classId))
				{
					filtered.Add(items[i]);
				}
			}
			items = filtered;
		}
	}
}


